package kltn.audioguidetravel;

import java.util.ArrayList;

import kltn.business.BAInfoTravel;
import kltn.data.Direction;
import kltn.data.InfoTravel;
import kltn.data.Place;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

public class WayActivity extends Activity{
	
	// Google Map
    private GoogleMap googleMap;
    private ListView lvWay;
    private Spinner spTo,spFrom;
    public ArrayList<String> list;
    public ArrayAdapter<String> adapter;
    ArrayList<Place> placesFrom;
    ArrayList<Place> placesTo;
    ArrayAdapter<Place> adapterFrom;
    ArrayAdapter<Place> adapterTo;
    BAInfoTravel baInfoTravel;
    ArrayList<Direction> directions= new ArrayList<>();
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_way);
		initial();
		loadMap();
		loadListView(placesFrom.get(0).getID(),placesTo.get(0).getID());
	}
	public void initial(){
		baInfoTravel= new BAInfoTravel(this);
		placesFrom=baInfoTravel.getPlaceFrom();
		placesTo=baInfoTravel.getPlaceTo(placesFrom.get(0).getID());
		//Cấu hình spinner đi
		adapterFrom= new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,placesFrom);
		adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spFrom=(Spinner) findViewById(R.id.spFrom);
		spFrom.setAdapter(adapterFrom);
		spFrom.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				placesTo=baInfoTravel.getPlaceTo(placesFrom.get(position).getID());
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		//Cấu hình spinner đến
		adapterTo= new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,placesTo);
		adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spTo=(Spinner) findViewById(R.id.spTo);
		spTo.setAdapter(adapterTo);
		spTo.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				loadListView(placesFrom.get(spFrom.getSelectedItemPosition()).getID(),placesTo.get(position).getID());
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
			}
		});
		//Cấu hình listview
		list= new ArrayList<>();
		adapter= new ArrayAdapter<String>(WayActivity.this,android.R.layout.simple_list_item_1,list);
		lvWay=(ListView) findViewById(R.id.lvWay);
		lvWay.setAdapter(adapter);
		lvWay.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int pos,
					long id) {
				Intent intent= new Intent(WayActivity.this, CustomActivity.class);
				intent.putExtra("direcID",directions.get(pos).getID());
				startActivity(intent);
			}
		});
	}
	@SuppressLint("NewApi")
	public void loadMap1(){
		if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.wMap)).getMap();
            MarkerOptions markerFrom = new MarkerOptions().position(new LatLng(10.8149624, 106.7107113)).title("HCM");
            MarkerOptions markerTo = new MarkerOptions().position(new LatLng(10.3508497, 107.0871609)).title("VT");
			googleMap.addMarker(markerTo);
			googleMap.addMarker(markerFrom);
			CameraPosition cameraPosition = new CameraPosition.Builder().target(
                    new LatLng(10.8499298, 106.7716219)).zoom(14).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
			///di chuyen camera chua toan bo marker
//			LatLngBounds.Builder builder = new LatLngBounds.Builder();
//			builder.include(markerTo.getPosition());
//			builder.include(markerFrom.getPosition());
//			LatLngBounds bounds = builder.build();
//			googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 3));
			
			googleMap.getUiSettings().setMyLocationButtonEnabled(true);
            googleMap.setMyLocationEnabled(true);
            ArrayList<InfoTravel> infoTravels=baInfoTravel.getAllInfoTravel();
    		for (InfoTravel i:infoTravels){
    			MarkerOptions marker=new MarkerOptions().position(i.getLatLng()).title("ID "+i.getID());
    			googleMap.addMarker(marker);
    		}
		}
	}
	@SuppressLint("NewApi")
	public void loadMap(){
		if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.wMap)).getMap();
            MarkerOptions markerFrom = new MarkerOptions().position(new LatLng(10.8149624, 106.7107113)).title("HCM");
            MarkerOptions markerTo = new MarkerOptions().position(new LatLng(10.3508497, 107.0871609)).title("VT");
			googleMap.addMarker(markerTo);
			googleMap.addMarker(markerFrom);
			CameraPosition cameraPosition = new CameraPosition.Builder().target(
                    new LatLng(10.8499298, 106.7716219)).zoom(14).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
			///di chuyen camera chua toan bo marker
//			LatLngBounds.Builder builder = new LatLngBounds.Builder();
//			builder.include(markerTo.getPosition());
//			builder.include(markerFrom.getPosition());
//			LatLngBounds bounds = builder.build();
//			googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 3));
			
			googleMap.getUiSettings().setMyLocationButtonEnabled(true);
            googleMap.setMyLocationEnabled(true);
            
            ArrayList<InfoTravel> infoTravels=baInfoTravel.getAllInfoTravel();
    		for (InfoTravel i:infoTravels){
    			MarkerOptions marker=new MarkerOptions().position(i.getLatLng()).title("ID "+i.getID());
    			googleMap.addMarker(marker);
    		}
		}
	}
	
	public void loadListView(int origin,int destination){
		directions=baInfoTravel.getDirections(origin,destination);
		list.clear();
		for (Direction di : directions) {
			Log.d("test",di.getDistance()+"m - "+di.getPoints().size()+"s");
			String distanse=di.getDistance();
			if(distanse.length()>3)
				distanse=distanse.substring(0,distanse.length()-3)+","+distanse.substring(distanse.length()-3,distanse.length()-2)+"km";
			else distanse+="m";
			list.add("Lái xe qua "+di.getSummary()+" - "+distanse);
		}
		adapter.notifyDataSetChanged();
		//Vẽ map
		int c=0;
		for (Direction direc:directions){
			ArrayList<LatLng> mang=direc.getPoints();
			PolylineOptions rectLine1 = new PolylineOptions().width(3).color(Color.BLUE); // Màu và độ rộng
			for(int i = 0 ; i < mang.size() ; i++) {         
	              rectLine1.add(mang.get(i));
  	        }	       
			c++;
			if (c%2==0)rectLine1.color(Color.RED);
  	        googleMap.addPolyline(rectLine1);
		}
	}
}
